// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/SingleMarkerWidget.h"
#include "Components/Border.h"
#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "Libraries/GameKitFL.h"
#include "Libraries/CommonLibrary.h"
#include "Materials/MaterialInstance.h"

#define LOCTEXT_NAMESPACE "BulletScars_Mission"

void USingleMarkerWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	MaxDisplayDistance = 99;
	TargetInfo = FTargetInfo();
}

void USingleMarkerWidget::NativeConstruct() {
	Super::NativeConstruct();
	ConstructWidget(TargetInfo);
}

void USingleMarkerWidget::ToggleMarker(EToggleVisibility InVisibility, bool InAnimate, bool InRemove)
{
	if (UGameKitFL::InGameMenuOpened()) {
		if (InRemove) {
			RemoveFromParent();
		}
	}
	else {
		if (InAnimate) {
			EUMGSequencePlayMode::Type PlayMode = InVisibility == EToggleVisibility::Visible ? EUMGSequencePlayMode::Reverse : EUMGSequencePlayMode::Forward;
			PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(),FText::FromString("Slide")), 0.f, 1.f, PlayMode);
		}
		if (InRemove) {
			if (!InAnimate && InRemove) {
				FTimerHandle UniqueHandle;
				FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &USingleMarkerWidget::RemoveFromParent);
				GetWorld()->GetTimerManager().SetTimer(UniqueHandle, RespawnDelegate, UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("Slide"))->GetEndTime() + 0.25, false);
			}
			else {
				RemoveFromParent();
			}
		}
	}
}

void USingleMarkerWidget::UpdateElevation(EElevationState ElevationState)
{
	switch (ElevationState) {
		case EElevationState::Select:
			break;
		case EElevationState::Down:
		case EElevationState::Up:
			Container->SetContentColorAndOpacity(FLinearColor(1.f, 1.f, 1.f, 0.5f));
			break;
		case EElevationState::Same:
			Container->SetContentColorAndOpacity(FLinearColor(1.f, 1.f, 1.f, 1.f));
			break;
		default:
			break;
	}
}

void USingleMarkerWidget::UpdateMarkerDistance(int32 LocDistance)
{
	FText MaxDistance, ResultText;
	MaxDistance = FText::AsNumber(FMath::Clamp(LocDistance, 1, MaxDisplayDistance));
	if (LocDistance <= MaxDisplayDistance) {
		ResultText = FText::Format(LOCTEXT("Amount", "{0}m"), MaxDistance);
	}
	else {
		ResultText = FText::Format(LOCTEXT("Max", "{0}+"), FText::AsNumber(MaxDisplayDistance));
	}
	Distance->SetText(ResultText);
}


void USingleMarkerWidget::ConstructWidget(const FTargetInfo& LocalTargetInfo) {
	if (TargetInfo == LocalTargetInfo) {

	}
	else { 
		TargetInfo = LocalTargetInfo; 
	}
	FMarkerCustomization Temp = TargetInfo.MarkerCustomization;
	DynamicBackground->SetVisibility(Temp.DynamicBackground ? ESlateVisibility::Collapsed : ESlateVisibility::SelfHitTestInvisible);
	FSlateBrush TempBrush = FSlateBrush();
	TempBrush.TintColor = FSlateColor(Temp.MarkerColor);
	UObject* Resource;
	if (Temp.UseMaterialInstance) Resource = Temp.MarkerMaterial;
	else Resource = Temp.MarkerTexture;
	TempBrush.SetResourceObject(Resource);
	Icon->SetBrush(TempBrush);
	DynamicBackground->SetBrushColor(Temp.DynamicBackgroundColor);
	BackgroundMask->SetBrushColor(Temp.BackgroundColor);
	Indicator->SetColorAndOpacity(Temp.IndicatorColor);
	if (UGameKitFL::InGameMenuOpened()) {
		Container->SetVisibility(ESlateVisibility::Collapsed);
	}
	else {
		PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("Slide")), 0.f, 1.f, EUMGSequencePlayMode::Reverse);
	}
}

#undef LOCTEXT_NAMESPACE